package com.dy.图.图的遍历;

public class 深度优先 {

    static int vex[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G'};

    public static void main(String[] args) {
        int vexNum = 7;
/**
 *             A
 *           / | \
 *         B   D  E
 *         |   | /
 *       G-F   C
 *
 */
        boolean[] visited = new boolean[vexNum];
        int arc[][] = {{0, 1, 0, 1, 1, 0, 0},
                {1, 0, 0, 0, 0, 1, 0},
                {0, 0, 0, 1, 1, 0, 0},
                {1, 0, 1, 0, 0, 0, 0},
                {1, 0, 1, 0, 0, 0, 0},
                {0, 1, 0, 0, 0, 0, 1},
                {0, 0, 0, 0, 0, 1, 0}
        };

        for (int i = 0; i < vexNum; i++) {
            if (!visited[i]) {
                DFS(i, arc, visited);
            }
        }

    }

    public static void DFS(int i, int[][] arc, boolean[] visited) {

        visited[i] = true;
        System.out.println((char) vex[i]);
        for (int j = 0; j < vex.length; j++) {
            if (visited[j] == false && arc[i][j] == 1) {
                DFS(j,arc,visited);
            }
        }

    }
}
